/**
 * 
 */
package dt.inlee.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.common.util.SystemResourceUtil;
import dt.inlee.common.util.TreeUtil;
import dt.inlee.common.vo.PageBean;
import dt.inlee.domain.SysOrganization;
import dt.inlee.domain.SysUser;
import dt.inlee.system.service.SysOrganizationService;
import dt.inlee.system.vo.SysOrganizationBean;
import dt.inlee.system.vo.SysUserBean;
import dt.inlee.system.vo.TreeNodeBean;

/**
 * @功能说明：组织机构管理控制层
 * @作者： 印鲜刚
 * @创建日期： 2010-5-18
 * @
 */
@Scope("prototype")
@Results( { @Result(name = "success", type = "json") })
@Controller("sysOrganizationAction")
public class SysOrganizationAction extends ActionSupport {
	private static final long serialVersionUID = 8900474342847117061L;
	private Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private SysOrganizationService sysOrganizationService;
	private String data;
	private Map<String, String> attr;
	private List<TreeNodeBean> children;
	private String state;
	private SysOrganizationBean sysOrganizationBean = new SysOrganizationBean();
	private List<SysUserBean> sysUserBeans = new ArrayList<SysUserBean>();
	private PageBean pageBean = new PageBean();
	//处理结果
	private boolean result = false;
	//新增节点ID值
	private String newId = null;
	//选中节点ID值
	private String selectId = null;

	// 移动菜单需要用到的参数
	private String oldNodeId, newNodeId, changeType;

	public String getOldNodeId() {
		return oldNodeId;
	}

	public void setOldNodeId(String oldNodeId) {
		this.oldNodeId = oldNodeId;
	}

	public String getNewNodeId() {
		return newNodeId;
	}

	public void setNewNodeId(String newNodeId) {
		this.newNodeId = newNodeId;
	}

	public String getChangeType() {
		return changeType;
	}

	public void setChangeType(String changeType) {
		this.changeType = changeType;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public Map<String, String> getAttr() {
		return attr;
	}

	public void setAttr(Map<String, String> attr) {
		this.attr = attr;
	}

	public List<TreeNodeBean> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNodeBean> children) {
		this.children = children;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public SysOrganizationBean getSysOrganizationBean() {
		return sysOrganizationBean;
	}

	public void setSysOrganizationBean(SysOrganizationBean sysOrganizationBean) {
		this.sysOrganizationBean = sysOrganizationBean;
	}

	public List<SysUserBean> getSysUserBeans() {
		return sysUserBeans;
	}

	public void setSysUserBeans(List<SysUserBean> sysUserBeans) {
		this.sysUserBeans = sysUserBeans;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public String getNewId() {
		return newId;
	}

	public void setNewId(String newId) {
		this.newId = newId;
	}

	public String getSelectId() {
		return selectId;
	}

	public void setSelectId(String selectId) {
		this.selectId = selectId;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	/**
	 * 显示树形组织机构
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "findSelectSysOrganizations", results = { @Result(type = "json") })
	public String findSelectSysOrganizations() throws FrameworkException, Exception {
		TreeNodeBean treeNodeBean = null;

		List<SysOrganization> sysOrganizations = sysOrganizationService.findAllSysOrganizations();
		treeNodeBean = TreeUtil.convertMenuTreeNode(sysOrganizations);

		children = treeNodeBean.getChildren();
		// 父节点
		data = treeNodeBean.getData();
		attr = treeNodeBean.getAttr();
		return SUCCESS;
	}

	/**
	 * 显示树形组织机构
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "findAllSysOrganizations", results = { @Result(type = "json") })
	public String findAllSysOrganizations() throws FrameworkException, Exception {
		TreeNodeBean treeNodeBean = null;
		List<SysOrganization> sysOrganizations = sysOrganizationService.findAllSysOrganizations();
		treeNodeBean = TreeUtil.convertMenuTreeNode(sysOrganizations);
		children = treeNodeBean.getChildren();
		// 父节点
		data = treeNodeBean.getData();
		attr = treeNodeBean.getAttr();

		return SUCCESS;
	}

	/**
	 * 修改组织机构功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "modifySysOrganization", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true", "includeProperties", "result,newId" }) })
	public String modifySysOrganization() throws FrameworkException {
		if (!BaseUtil.isEmpty(this.sysOrganizationService.findSysOrganizationByIdAndName(this.sysOrganizationBean
				.getId(), this.sysOrganizationBean.getName()))) {
			newId = "none";
			return SUCCESS;
		}
		SysOrganization sysOrganization = this.sysOrganizationService.findSysOrganizationById(this.sysOrganizationBean
				.getId());
		Integer orderNo = sysOrganization.getOrderNo();
		BeanUtils.copyProperties(this.sysOrganizationBean, sysOrganization);
		sysOrganization.setOrderNo(orderNo);
		this.sysOrganizationService.modifySysOrganization(sysOrganization);
		result = true;
		return SUCCESS;
	}

	/**
	 * 组织机构数删除功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "deleteSysOrganization", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true", "includeProperties", "result" }) })
	public String deleteSysOrganization() throws FrameworkException {
		this.sysOrganizationService.deleteAllSysOrganizationsById(this.sysOrganizationBean.getId());
		result = true;
		return SUCCESS;
	}

	/**
	 * 同级组织机构新增功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "sameSaveSysOrganization", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true", "includeProperties", "result,newId" }) })
	public String sameSaveSysOrganization() throws FrameworkException {
		if (!BaseUtil
				.isEmpty(this.sysOrganizationService.findSysOrganizationByName(this.sysOrganizationBean.getName()))) {
			newId = "none";
			return SUCCESS;
		}
		SysOrganization sysOrganization = new SysOrganization();
		BeanUtils.copyProperties(this.sysOrganizationBean, sysOrganization);
		logger.debug("selectId=" + this.getSelectId());
		this.sysOrganizationService.sameSaveSysOrganization(sysOrganization, this.getSelectId());
		newId = sysOrganization.getId();
		result = true;
		return SUCCESS;
	}

	/**
	 * 次级组织机构新增功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "secondarySaveSysOrganization", results = { @Result(type = "json", params = {
			"excludeNullProperties", "true", "includeProperties", "result,newId" }) })
	public String secondarySaveSysOrganization() throws FrameworkException {
		if (!BaseUtil
				.isEmpty(this.sysOrganizationService.findSysOrganizationByName(this.sysOrganizationBean.getName()))) {
			newId = "none";
			return SUCCESS;
		}
		SysOrganization sysOrganization = new SysOrganization();
		BeanUtils.copyProperties(this.sysOrganizationBean, sysOrganization);
		logger.debug("selectId=" + this.getSelectId());
		this.sysOrganizationService.secondarySaveSysOrganization(sysOrganization, this.getSelectId());
		newId = sysOrganization.getId();
		result = true;
		return SUCCESS;
	}

	/**
	 * 通过组织机构ID获取获取组织机构及其关联人员信息
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "findSysUsersBySysOrganizationId", results = { @Result(type = "json", params = {
			"excludeNullProperties", "true", "excludeProperties", "sysOrganizationBean,sysOrganizationBeans" }) })
	@SkipValidation()
	public String findSysUsersBySysOrganizationId() throws FrameworkException {
		this.pageBean.init(this.sysOrganizationService.countSysUsersByOrgId(this.sysOrganizationBean.getId()));
		List<SysUser> sysUsers = this.sysOrganizationService.findSysUsersBySysOrganizationId(this.sysOrganizationBean
				.getId(), this.pageBean.getCurPage(), this.pageBean.getPageSize());
		for (SysUser sysUser : sysUsers) {
			SysUserBean sysUserBean = new SysUserBean();
			BeanUtils.copyProperties(sysUser, sysUserBean);
			sysUserBean.setGender(SystemResourceUtil.getInstance().getCnNameOfBusinessDictionary("gender",
					sysUserBean.getGender()));
			this.sysUserBeans.add(sysUserBean);
		}
		return SUCCESS;
	}

	/**
	 * 根据组织机构ID修改原有人员
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "setSysUserBySysOrganizationId", results = { @Result(type = "json", params = {
			"excludeNullProperties", "true", "includeProperties", "result" }) })
	@SkipValidation()
	public String setSysUserBySysOrganizationId() throws FrameworkException {
		this.sysOrganizationService.setSysUser(this.sysOrganizationBean.getId(), BaseUtil
				.isEmpty(this.sysOrganizationBean.getSysUserIDs()) ? null : this.sysOrganizationBean.getSysUserIDs()
				.split(","));
		result = true;
		return SUCCESS;
	}

	/**
	 * 根据组织机构ID新增人员
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "setNewSysUserBySysOrganizationId", results = { @Result(type = "json", params = {
			"excludeNullProperties", "true", "includeProperties", "result" }) })
	@SkipValidation()
	public String setNewSysUserBySysOrganizationId() throws FrameworkException {
		this.sysOrganizationService.setNewSysUser(this.sysOrganizationBean.getId(), BaseUtil
				.isEmpty(this.sysOrganizationBean.getSysUserIDs()) ? null : this.sysOrganizationBean.getSysUserIDs()
				.split(","));
		result = true;
		return SUCCESS;
	}

	/**
	 * 根据组织机构移动功能
	 * 
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "moveSysOrganization", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true", "includeProperties", "result" }) })
	public String moveSysOrganization() throws FrameworkException {

		result = false;
		logger.debug("→→→↓↓→→拖动测试←←↓↓←←←←←");
		logger.debug("oldid:" + oldNodeId);
		logger.debug("newid:" + newNodeId);
		logger.debug("TYPE:" + changeType);
		logger.debug("→→→↑↑→→拖动测试←←↑↑←←←←←");
		// 做校验，判断变更类型
		if ("inside".equals(changeType) == false && "before".equals(changeType) == false
				&& "after".equals(changeType) == false) {
			result = false;
			return SUCCESS;
		}
		result = this.sysOrganizationService.moveNode(oldNodeId, newNodeId, changeType);
		return SUCCESS;
	}

}
